const app = getApp();
import Toast from 'tdesign-miniprogram/toast/index';
import Dialog from 'tdesign-miniprogram/dialog/index';
Page({
    toast(option) {
		Toast({
			context: this,
			selector: '#t-toast',
			...option,
		});
	},
    /**
     * 页面的初始数据
     */
    data: {
        // 是否显示社区选择器，扫码进来会携带社区码，无需显示，如果没有填报过就提示扫社区二维码，填报过出现社区选择器进行填报
        isShowArea: false,
        areaVisible: false,
        areaOptions: [],
        empty: false,
        wish: {
            title: ``,
            content: ``,
            images: ``,
            type: false,
            isAnonymous: false,
            areaCode: ``,
            areaLabel: ``,
        },
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // 小程序进入该页面
        if (options.type == 1) {
            // 拉取该市民填报的社区
            app.httpGet(`wish/area/${wx.getStorageSync('openid')}`).then((res) => {
                if (res.data && res.data.length > 0) {
                    this.setData({
                        areaOptions: res.data,
                        isShowArea: true
                    });
                } else {
                    // 提示扫社区二维码进行填报
                    this.setData({
                        empty: true
                    });
                }
            });
        } else {
            console.log(options);
        }
    },

    chooseArea() {
        this.setData({
            areaVisible: true
        });
    },

    onCancelArea() {
        this.setData({
            areaVisible: false
        });
    },

    /**
     * 选择社区
     * @param {} e 
     */
    onConfirmArea(e) {
        let wish = this.data.wish;
        wish.areaCode = e.detail.value[0];
        wish.areaLabel = e.detail.label[0];
        this.setData({ wish, areaVisible: false });
    },

    titleChange(e) {
        let wish = this.data.wish;
        wish.title = e.detail.value;
        this.setData({ wish });
    },

    contentChange(e) {
        let wish = this.data.wish;
        wish.content = e.detail.value;
        this.setData({ wish });
    },

    /**
     * 文件上传组件
     * @param {*} e 
     */
    changeImages(e) {
        let wish = this.data.wish;
        wish.images = e.detail;
        this.setData({ wish });
    },

    handleChange(e) {
        let wish = this.data.wish;
        if (e.target.dataset.type == '1') {
            wish.type = e.detail.value;
            this.setData({ wish });
        } else if (e.target.dataset.type == '2') {
            wish.isAnonymous = e.detail.value;
            this.setData({ wish });
        }
    },

    reset() {
        this.setData({ wish: {
            title: ``,
            content: ``,
            images: ``,
            type: false,
            isAnonymous: false,
            areaCode: ``,
            areaLabel: ``,
        } });
    },

    /**
     * 提交心愿填报
     */
    submit() {
        const wish = this.data.wish;
        if (!wish.areaCode && this.data.isShowArea && !this.data.empty) {
            return this.toast({ message: '请选择社区填报或扫社区二维码填报', theme: 'fail', duration: 3000 });
        }
        if (!wish.title) {
            return this.toast({ message: '标题不能为空', theme: 'fail' });
        }
        if (!wish.images) {
            return this.toast({ message: '请上传您要填报的图片或视频', theme: 'fail', duration: 3000 });
        }
        if (!wish.content) {
            return this.toast({ message: '请输入您填报的具体内容', theme: 'fail' });
        }
        // 不是匿名填报
        if (!wish.isAnonymous) {
            // 判断用户信息是否存在
            const userInfo = wx.getStorageSync('userInfo');
            if (!userInfo.nickName || !userInfo.avatar || !userInfo.sex) {
                const dialogConfig = {
                    context: this,
                    title: '提示',
                    content: '您还未补全基本信息，请补全后提交。',
                    confirmBtn: '确认',
                    cancelBtn: '取消',
                };
                Dialog.confirm(dialogConfig).then(() => {
                    if (wx.getStorageSync('openid')) {
                        wx.navigateTo({ url: '/pages/user/user-info/index?type=1' });
                    } else {
                        wx.navigateTo({ url: '/pages/user/user' });
                    }
                });
            } else {
                wish.openid = wx.getStorageSync('openid');
                this.insertWish(wish);
            }
        } else {
            this.insertWish(wish);
        }
    },

    insertWish(wish) {
        if (!wish.openid) {
            app.httpLogin().then(() => {
                wish.openid = wx.getStorageSync('openid');
            });
        }
        // 心愿填报
        app.httpPost('wish/insert', wish).then((res) => {
            this.toast({ message: res.message, theme: 'success', duration: 2000 });
            wx.redirectTo({
                url: '/pages/index/index',
            });
        });
    }

});